<?php
class Picturethumb{
	private $dir;
	private $currentpath;
	private static $_info;

	function __construct() {
	}


	function image($imagepath,$filename,$width=130,$height=97){
		$filetype = explode('.',$filename);
		$savePath = str_replace($filename,$filetype[0] . '_'.$width. 'X' . $height . '.' . $filetype[1],$imagepath);

		$_img = getimagesize($imagepath);
		self::$_info['width'] = $_img[0]; //照片宽
		self::$_info['height'] = $_img[1]; //照片宽
		self::$_info['filetype'] = $_img[2];//文件类型
		self::$_info['minetype'] = $_img['mime'];//文件格式
		$this->createImage($imagepath,self::$_info['filetype']);
	

		//$savePath ='tn_'.$savePath; 
		$imgSize['width'] = $width;
		$imgSize['height'] = $height;
		if (function_exists("imagecreatetruecolor")) {
				$newImg = imagecreatetruecolor($imgSize['width'], $imgSize['height']);
				ImageCopyResampled($newImg, self::$_info['image'], 0, 0, 0, 0, $imgSize['width'], $imgSize['height'], self::$_info['width'], self::$_info['height']);
		} else {
				$newImg = imagecreatetruecolor($imgSize['width'], $imgSize['height']);
				ImageCopyResampled($newImg, self::$_info['image'], 0, 0, 0, 0, $imgSize['width'], $imgSize['height'], self::$_info['width'], self::$_info['height']);
		}

		if (file_exists($savePath)) @unlink($savePath);
		ImageJPEG($newImg,$savePath);
		ImageDestroy($newImg);
	}

	private function createImage($photo,$filetype){
		$photopath = $photo;
		switch($filetype){
			case '1'://gif
				self::$_info['image'] = imagecreatefromgif($photo);break;
			case '2'://jpeg,jpe
				self::$_info['image'] = imagecreatefromjpeg($photo);break;
			case '3'://png
				self::$_info['image'] = imagecreatefrompng($photo);break;
			case '6'://bmp
				$photopath = '';
				include_once APPPATH . DS . 'helpers' . DS .'imgcreatebmp_helper.php';
				self::$_info['image'] = imagecreatefrombmp($photo);break;
			default:self::$_info['image']='';
		}
		return $photopath;
	}
}